﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;

namespace Kovan.DAL.UserCenter
{
    public class DActUser : IDAL<act_user>
    {

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public act_user GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_user.FirstOrDefault(c => c.user_id.Value.Equals(id));
            }
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public act_user GetModel(string loginName,string pwd)
        {
            using (var db = new MysqlContext())
            {
                return db.act_user.FirstOrDefault(c => c.login_name.Equals(loginName) && c.password.Equals(pwd));
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Add(act_user model)
        {
            using (var db = new MysqlContext())
            {
                db.act_user.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_user.FirstOrDefault(a => a.user_id == id);
                db.act_user.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var userid = long.Parse(id);
                    var model = db.act_user.FirstOrDefault(a => a.user_id == userid);
                    if (model != null)
                    {
                        db.act_user.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Update(act_user model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_user>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
    }
}
